package cn.edu.ctbu.demo1.webapi;
import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Course;
import cn.edu.ctbu.demo1.service.CourseService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@RequestMapping("/webapi/course")
public class CourseRestController {
    @Autowired
    CourseService courseService;
    /**
     * 读取的url:/webapi/course/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Course course = courseService.findById(id);
        return RUtil.success(course);
    }

    /**
     * 新增学生的方法
     * @param course
     * @return
     */
    @PostMapping("/insert")
    public R insert(Course course){

        Course course1 = courseService.insert(course);
        return RUtil.success(course1);
    }

    /**
     * 更新学生的方法
     * @param course
     * @return
     */
    @PutMapping ("/update")
    public R update(Course course){
        courseService.update(course);
        return RUtil.success(course);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        courseService.delete(id);
        return RUtil.success(null);
    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="select",defaultValue = "")Integer select,
            @RequestParam(value ="content",defaultValue = "")String content
    ){
        Page<Course> coursePage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable  pageable = PageRequest.of(page,size,Sort.by(Sort.Direction.ASC,"id"));
            coursePage = courseService.findAll(pageable);
        }
        else {
            Course course = new Course();
            Pageable pageable = PageRequest.of(0,10, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher;
            /**
             * 如果下拉框选中的是课程编号
             */
            if(select.equals(1))
            {
                course.setNo(content);
                matcher = ExampleMatcher.matching().
                        withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            }
            /**
             * 如果下拉框选中的是课程名称
             */
            else {
                course.setName(content);
                matcher = ExampleMatcher.matching()//模糊查询
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            }

            Example<Course> example = Example.of(course,matcher);
            coursePage = courseService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(coursePage.getContent(),coursePage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}


